package com.autocar.reader.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.autocar.reader.R;
import com.autocar.reader.bean.ProductBean;
import com.autocar.reader.net.ImageLoader;
import com.autocar.reader.net.ImageLoader.ImageCallback;
import com.autocar.reader.util.ConstantData;
import com.autocar.reader.util.ImageUtils;
import com.org.any.autocar.fragment.IopenProduct;

public class ProductSubAdapter extends BaseAdapter implements ImageCallback {

	private LayoutInflater inflater;
	private GridView rootView;
	private ImageLoader load;
	private List<ProductBean> listItems;
	private IopenProduct iopen;

	public ProductSubAdapter(Context context, GridView rootView,
			IopenProduct iopen) {
		inflater = LayoutInflater.from(context);
		this.rootView = rootView;
		this.iopen = iopen;
		load = new ImageLoader(this);
	}

	public void setProductList(List<ProductBean> listItems) {
		this.listItems = listItems;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listItems == null ? 0 : listItems.size();
	}

	@Override
	public ProductBean getItem(int position) {
		return listItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHodler holder;
		if (convertView == null) {
			holder = new ViewHodler();
			convertView = inflater.inflate(
					R.layout.product_sub_grid_item_layout, null);
			holder.picIv = (ImageView) convertView
					.findViewById(R.id.product_sub_grid_iv_pic);
			holder.titleTv = (TextView) convertView
					.findViewById(R.id.product_sub_grid_tv_title);
			convertView.setTag(holder);
		} else {
			holder = (ViewHodler) convertView.getTag();
		}
		final ProductBean bean = getItem(position);
		holder.titleTv.setText(bean.getName());
//		final String url = bean.getS_pic();
//		holder.picIv.setTag(url+ position);
//		String filePath = ConstantData.getImagePath(url);
//		Bitmap bitmap = ConstantData.getBitmapBypath(filePath);
//		holder.picIv.setImageBitmap(bitmap);
//		if (bitmap == null) {
//			holder.picIv.setImageResource(R.drawable.no_img_0);
//			load.loadDrawable(url, filePath);
//		}
		ImageUtils.fb.display(holder.picIv, bean.getS_pic());
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (iopen != null) {
					iopen.opentProduct(bean);
				}
			}
		});
		return convertView;
	}

	class ViewHodler {
		TextView titleTv;
		ImageView picIv;
	}

	@Override
	public void imageLoaded(String filePath, String imageUrl) {
		ImageView view = (ImageView) rootView.findViewWithTag(imageUrl);
		if (view != null) {
			view.setImageBitmap(ConstantData.getBitmapBypath(filePath));
		}
	}
}
